package com.actor.develop_helper.activity;

import android.os.Bundle;

import com.actor.develop_helper.adapter.SpineSkeletalAdapter;
import com.actor.develop_helper.databinding.ActivitySpineSkeletalBinding;
import com.actor.myandroidframework.utils.LogUtils;
import com.badlogic.gdx.utils.Array;
import com.esotericsoftware.spine.Animation;
import com.esotericsoftware.spine.AnimationState;
import com.esotericsoftware.spine.AnimationStateData;
import com.esotericsoftware.spine.SkeletonData;
import com.esotericsoftware.spine.android.SpineController;
import com.esotericsoftware.spine.android.callbacks.SpineControllerCallback;

import java.util.List;

/**
 * description: Spine 播放骨骼动画示例
 * company    :
 * @author    : ldf
 * date       : 2025/1/23 on 17:51
 */
public class SpineSkeletalActivity extends BaseActivity<ActivitySpineSkeletalBinding> {

    private final SpineSkeletalAdapter mAdapter = new SpineSkeletalAdapter();
    private SpineController spineController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(" Spine 播放骨骼动画");
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Animation item = mAdapter.getItem(position);
            AnimationState animationState = spineController.getAnimationState();
            animationState.setAnimation(0, item.getName(), true);
//            animationState.addAnimation(0, "walk", true, 0f);
        });
        viewBinding.recyclerView.setAdapter(mAdapter);

        spineController = new SpineController(new SpineControllerCallback() {
            @Override
            public void execute(SpineController controller) {
                LogUtils.error("进入了execute()"); //只进入1次
                AnimationStateData animationStateData = controller.getAnimationStateData();
                SkeletonData skeletonData = animationStateData.getSkeletonData();
                Array<Animation> animations = skeletonData.getAnimations();
                //java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.esotericsoftware.spine.Animation[]
//                mAdapter.setList(Arrays.asList(animations.items));

                List<Animation> data = mAdapter.getData();
                data.clear();
                //动画列表
                for (Animation animation : animations) {
//                    LogUtils.errorFormat("name=%s, Duration=%f, Timelines.size=%d", animation.getName(), animation.getDuration(), animation.getTimelines().size);
                    data.add(animation);
                }
                mAdapter.setList(data);

                AnimationState animationState = controller.getAnimationState();
                animationState.setAnimation(0, "portal", false);
                animationState.addAnimation(0, "run", true, 0f);
            }
        });
        viewBinding.spineView.setController(spineController);
        viewBinding.spineView.loadFromAsset("skeletal/spineboy.atlas", "skeletal/spineboy-pro.json");
    }
}